﻿using System;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;


namespace Pipo.DevExExtensions{
    public class XtraGridGridView
    {
          
        public static  GridRowType  GetRowTypeByRowHandle (int rowHandle, GridView gridview) {
            if (gridview.IsGroupRow(rowHandle))
                return GridRowType.Grouprow;
            if (rowHandle == GridControl.AutoFilterRowHandle)
                return GridRowType.FilterRow;
            if (rowHandle == GridControl.NewItemRowHandle)
                return GridRowType.NewItemRow;
            if (rowHandle == GridControl.InvalidRowHandle)
                return GridRowType.InvalidRow;
            return GridRowType.DataRow;
        }

        /// <summary>
        /// Accepts gridview object and return the rowhandle of the focusedrow
        /// </summary>
        /// <param name="gridView"></param>
        /// <returns> Returns the rowhandle of valid focused data row, otherwise.. null</returns>
        /// <remarks></remarks>
        public static  int? GetValidFocusedDataRow ( GridView gridView){
        switch ( GetRowTypeByRowHandle( gridView.FocusedRowHandle, gridView) ){
                    case GridRowType.Grouprow:
                return null;
                        case GridRowType.FilterRow:
                        return null;
                    case GridRowType.NewItemRow:
                        return null;
                    case GridRowType.InvalidRow:
                        return null;
                    case GridRowType.DataRow:
                return gridView.FocusedRowHandle;
                    default:
                        throw new ArgumentOutOfRangeException();
                
            }
        }
    }
}